﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace Client
{
    class ClsServerHandler
    {
        NetworkStream netStream;
        StreamReader reader;
        StreamWriter skriver;

        ClsClientMonitor klientMonitor;
        bool stop = false;

        public ClsServerHandler(NetworkStream stream, ClsClientMonitor klientMonitor)
        {
            this.netStream = stream;
            this.klientMonitor = klientMonitor;                   
        }

        public void Skriver(string besked)
        {
            skriver = new StreamWriter(netStream);

            skriver.WriteLine(besked);
            skriver.Flush();
        }

        // Lytter efter besked
        public void Læser()
        {
            while (true)
            {
                reader = new StreamReader(netStream);
                try
                {
                    while (!stop)
                    {
                        string besked = reader.ReadLine();
                        klientMonitor.ModtagBeskedFraServer(besked);
                    }
                }

                catch
                {

                }
            }
        } 
    }
}
